<?php
/**
 * Helper for setting and retrieving stylesheets in conjunction with the css
 * minifier library.
 *
 * @package    Clients
 * @subpackage View Helpers
 * @copyright  (c) 2011 Coalition Technologies <joel@coalitiontechnologies.com>
 * @author     Luke Visinoni <luke.visinoni@gmail.com>
 * @version    $Id$
 */
class Visi_View_Helper_HeadLinkMin extends Zend_View_Helper_HeadLink {

    /**
     * A list of the stylesheets to minify
     */
    protected $_minifiedStylesheets = array();
    
    /**
     * @var string registry key
     */
    protected $_regKey = 'Visi_View_Helper_HeadLinkMin';
    
    /**
     * Append stylesheet(s) using the css minify library
     */
    public function appendMinifiedStylesheet($name, Array $hrefs, $media = null, $conditionalStylesheet = null, $extras = null) {
    
        if (array_key_exists($name, $this->_minifiedStylesheets)) {
            if (array_key_exists('hrefs', $this->_minifiedStylesheets[$name])) {
                $hrefs = array_merge($this->_minifiedStylesheets[$name]['hrefs'], $hrefs);
            }
        }
        if (is_null($media)) $media = 'screen';
        $this->_minifiedStylesheets[$name] = array(
            'hrefs' => $hrefs,
            'media' => $media,
            'conditional' => $conditionalStylesheet,
            'extras' => $extras,
        );
        return $this;
    
    }
    
    /**
     * Adds a file to the specified minified stylesheet
     * @todo Maybe the first part of this (the array key exists part) should use $this->appendMinifiedStylesheet()
     */
    public function addFile($name, $file) {
    
        if (!array_key_exists($name, $this->_minifiedStylesheets)) {
            $this->_minifiedStylesheets[$name] = array(
                'hrefs' => array(),
                'media' => null,
                'conditional' => null,
                'extras' => null,
            );
        }
        $this->_minifiedStylesheets[$name]['hrefs'][] = $file;
        return $this;
    
    }
    
    /**
     * Output the minified head link (stylesheet)
     */
    public function headLinkMin(array $attributes = null, $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND) {
    
        return $this->headLink($attributes, $placement);
    
    }
    
    /**
     * Render link elements as string
     *
     * @param  string|int $indent
     * @return string
     * @todo   See if it is possible to call parent::toString() rather than recreating the whole method
     */
    public function toString($indent = null) {
    
        $indent = (null !== $indent)
                ? $this->getWhitespace($indent)
                : $this->getIndent();
        foreach ($this->_minifiedStylesheets as $name => $stylesheet) {
            $files = implode(',', array_unique($stylesheet['hrefs']));
            $href = '/min/f=' . $files;
            $this->appendStylesheet($href, $stylesheet['media'], $stylesheet['conditional'], $stylesheet['extras']);
        }
        $items = array();
        $this->getContainer()->ksort();
        foreach ($this as $item) {
            $items[] = $this->itemToString($item);
        }
        return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
    
    }

}
